#include"channel.h"
#include"EventLoop.h"
#include"myepoll.h"
#include<functional>

MyChannel::MyChannel(/* args */)
{
}

MyChannel::MyChannel(EventLoop *_loop, int _fd)
{
    loop = _loop;
    fd = _fd;
}

MyChannel::~MyChannel()
{
    this->inEpoll = false;
    if(fd!=-1){
        close(fd);
        fd = -1;
    }
}

int MyChannel::getFd()
{
    return fd;
}

void MyChannel::enableReading()
{
    //监听，低电平触发
    flagReadorWrite = true;
    events &= ~(EPOLLOUT|EPOLLET);
    events |= EPOLLIN|EPOLLET;
    loop->updateChannel(this);
}

void MyChannel::enableWriting()
{
    //监听写，低电平触发
    flagReadorWrite = false;
    events &= ~(EPOLLIN|EPOLLET);
    events |= EPOLLOUT|EPOLLET;
    loop->updateChannel(this);
}

uint32_t MyChannel::getEvents()
{
    return events;
}

uint32_t MyChannel::getRevents()
{
    return revents;
}

bool MyChannel::getInEpoll()
{
    return inEpoll;
}

void MyChannel::setInEpoll()
{
    inEpoll = true;
}

void MyChannel::setInEpoll(bool falg)
{
    inEpoll = falg;
}

void MyChannel::setRevents(uint32_t ev) 
{
    revents = ev;
}

void MyChannel::headleEvent()
{
    if(flagReadorWrite){
        readcallBack();
    }
    else{
        writecallBack();
    }
}

void MyChannel::setWriteCallBack(std::function<void()>_cb)
{
    writecallBack = _cb;
}

void MyChannel::setReadCallBack(std::function<void()> _cb)
{
    readcallBack = _cb;
}
